/*
 * InputHandler.cpp
 *
 *  Created on: 2010-06-14
 *      Author: Emile
 */

#include "InputHandler.h"
#include <iostream>
using namespace std;
class Unit;

InputHandler::InputHandler() {
}

InputHandler::~InputHandler() {

}

bool InputHandler::handleInput(Unit* player) {
	//Get the keystates, used to know if a key is currently pressed
	Uint8 *keystates = SDL_GetKeyState(NULL);
	SDL_Event event;

	// Key event : This "if" returns true only if a key have been pressed
	if (SDL_PollEvent(&event)) {
		if (event.type == SDL_QUIT) {
			//Quit the program
			return true;
		}
		//if (event.key.keysym.sym != SDLK_LAST) {
			uint32_t eventKey = event.key.keysym.sym;
			//If a key was pressed
			if (event.type == SDL_KEYDOWN) {
				//cout << "key pressed = " << event.key.keysym.sym << endl;
				//Set the proper message surface
				switch (eventKey) {

				case SDLK_LEFT:
					if (!keystates[SDLK_RIGHT])
					player->getState()->moveLeft(player);
					break;
				case SDLK_RIGHT:
					if (!keystates[SDLK_LEFT])
						player->getState()->moveRight(player);
					break;
			    case SDLK_SPACE:
                       player->getState()->shoot(player);
				}
			}

			if (event.type == SDL_KEYUP) {
				//Set the proper message surface
				switch (eventKey) {
				case SDLK_LEFT:
					if (!keystates[SDLK_RIGHT])
						player->getState()->becomeIdle(player);
					break;
				case SDLK_RIGHT:
					if (!keystates[SDLK_LEFT])
						player->getState()->becomeIdle(player);
					break;


				}
			//}
		}// end here if event.key.keysym.sym == SDLK_LAST that is not handle in switch
	}// end here if no keys is pressed

	/*if (keystates[SDLK_LEFT]) {
		((Megaman*) environment->getHero())->runLeft();
	} else if (keystates[SDLK_RIGHT]) {
		((Megaman*) environment->getHero())->runRight();
	}*/

	// End of the key handler




	return false;
}

